import requests
import json
import time
import urllib.parse
import hashlib
from Crypto.Cipher import AES
import base64

def pkcs5_pad(data: bytes) -> bytes:
    pad_len = 16 - len(data) % 16
    return data + bytes([pad_len] * pad_len)

def aes_encrypt_to_base64(data: str, key: str) -> str:
    key_bytes = key.encode('utf-8')[:16]
    key_bytes = key_bytes.ljust(16, b'\x00')
    cipher = AES.new(key_bytes, AES.MODE_ECB)
    padded_data = pkcs5_pad(data.encode('utf-8'))
    encrypted = cipher.encrypt(padded_data)
    return base64.b64encode(encrypted).decode('utf-8')

def generate_clicode(android_id, msa_oa_id, net="wifi", screen_size="1080_1920"):
    device_info = {
        "_android_id": android_id,
        "_msa_oa_id": msa_oa_id,
        "_net": net,
        "_bsize": screen_size,
        "_imei": msa_oa_id
    }
    json_str = json.dumps(device_info, separators=(',', ':'))
    key = "amJKJsirodlsd129"
    cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB)
    pad_length = 16 - len(json_str.encode('utf-8')) % 16
    padded = json_str.encode('utf-8') + bytes([pad_length]*pad_length)
    encrypted_data = cipher.encrypt(padded)
    base64_str = base64.b64encode(encrypted_data).decode()
    return urllib.parse.quote(base64_str)

def generate_mcode(appid, dev, udid, version):
    concatenated = appid + dev + udid + version
    with_salt = concatenated.lower() + "IO2#!spopqd"
    hash1 = hashlib.md5(with_salt.encode('utf-8')).hexdigest()
    hash2 = hashlib.md5(hash1.encode('utf-8')).hexdigest()
    hash3 = hashlib.md5(hash2.encode('utf-8')).hexdigest()
    return hash3.upper()[:8]

def build_str2(android_id, msa_oa_id, mcode, clicode, udid, main_time=None):
    t_now = main_time or int(time.time())
    t_str = str(t_now)
    sci_str = "{:.8E}".format(time.time())

    base_part = "_appid\\u003dAndroidPhone\\u0026_brand\\u003dRedmi"
    clicode_part = "\\u0026_clicode\\u003d" + clicode
    tail = (
        "\\u0026_dev\\u003d39"
        "\\u0026_mcode\\u003d" + mcode +
        "\\u0026_only_read\\u003dN"
        "\\u0026_os\\u003d13_23013RK75C"
        "\\u0026_os_name\\u003d23013RK75C"
        "\\u0026_udid\\u003d" + udid +
        "\\u0026_v\\u003d9.1.5"
        "\\u0026_version\\u003d9.08"
        "\\u0026appType\\u003dnormal"
    )

    url1 = (
        "https://iphone.myzaker.com/zaker/interaction.php?" + base_part + clicode_part + tail
    )
    url2 = (
        "https://iphone.myzaker.com/zaker/app_style.php?" + base_part + clicode_part +
        "\\u0026_flag\\u003d9f0369ff51876206a9b5dd33b6d91686" +
        tail +
        f"\\u0026_time\\u003d{t_str}" +
        "\\u0026sign_arg\\u003dapp_style|_appid,_nudid,_udid,_uid,_version"
    )
    url3 = (
        "https://iphone.myzaker.com/zaker/ar_data/ar_list.php?" + base_part + clicode_part + tail
    )

    str2_obj = {
        "bd_return_size": "71.5791",
        "bd_net_type": "wifi",
        "bd_return_time": "0.358",
        "bd_dtime": t_str,
        "list": [
            {
                "local_ip": "",
                "url": url1,
                "net_type": "wifi",
                "req_type": "local",
                "e_type": "2",
                "return_time": "3.23",
                "dtime": sci_str
            },
            {
                "local_ip": "",
                "url": url2,
                "net_type": "wifi",
                "req_type": "local",
                "e_type": "2",
                "return_time": "3.26",
                "dtime": sci_str
            },
            {
                "local_ip": "",
                "url": url3,
                "net_type": "wifi",
                "req_type": "local",
                "e_type": "2",
                "return_time": "3.363",
                "dtime": sci_str
            }
        ]
    }
    str2 = json.dumps(str2_obj, ensure_ascii=False, separators=(',', ':'))
    return str2, t_str

def main():
    android_id = "b9d0a4eaadbef9d1"
    msa_oa_id = "d4dcd1df6253de3b"
    udid = "1fb67af59b5699a6"
    appid = "AndroidPhone"
    dev = "39"
    version = "9.08"
    v = "9.1.5"
    key = "amJKJsirodlsd129"

    mcode = generate_mcode(appid, dev, udid, v)
    clicode = generate_clicode(android_id, msa_oa_id)
    print("clicode:", clicode)
    print("mcode:", mcode)

    main_time = int(time.time())
    str2, t_str = build_str2(android_id, msa_oa_id, mcode, clicode, udid, main_time)
    data_code = aes_encrypt_to_base64(str2, key)

    url = "https://iphone.myzaker.com/urlcheck/app_net.php"
    headers = {
        "Host": "iphone.myzaker.com",
        "User-Agent": "okhttp/4.2.2"
    }
    params = {
        "_no_track": "Y",
        "_time": t_str
    }
    data = {
        "data_code": data_code,
        "_os": "13_23013RK75C",
        "_icode": msa_oa_id,
        "_msa_oa_id": msa_oa_id,
        "_os_name": "23013RK75C",
        "_mcode": mcode,
        "_net": "wifi",
        "_bsize": "1080_1920",
        "_only_read": "N",
        "appType": "normal",
        "_v": v,
        "_udid": udid,
        "_dev": dev,
        "_appid": appid,
        "_android_id": android_id,
        "_version": version,
        "_brand": "Redmi"
    }

    resp = requests.post(url, headers=headers, params=params, data=data)
    print(resp.status_code)
    print(resp.text)

if __name__ == "__main__":
    main()